// pages/address/address.js
import util from "../../utils/util"
import Api from "../../utils/api"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        radio: 1,
        userinfos: [],
        choose:false
    },
    methods: {},
    //跳转至地址编辑页面
    modifyAddress: function (event) {
        var id = event.target.dataset.id;
        var userinfoStr = this.data.userinfos[id];
        var userinfo = JSON.stringify(userinfoStr);
        wx.navigateTo({
            url: '../addAddress/addAddress?status=modify&userinfo=' + userinfo,
        })
    },
    //新增地址
    addAddress: function () {
        wx.navigateTo({
            url: '../addAddress/addAddress?status=add',
        })
    },
    onChange(event) {
        this.setData({
            radio: event.detail,
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var type = options.type;
        if(type){
            this.setData({
                choose: options.type
            })
        }
        this.getAddressList();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        
    },
    getAddressList(){
        let that = this;
        util.request(Api.wx.AddressList).then(function (res) {
            if (res.errno === 0) {
                that.setData({
                    userinfos: res.data.list
                });
            }
        });
    },
    chooseAddress(){
        var currentPage = getCurrentPages();
        var prevPage = currentPage[currentPage.length - 2]; //上一个页面
        var userinfo = this.data.userinfos[this.data.radio-1];
        prevPage.setData({
            status: 'add',
            userinfo: userinfo
        })
        try {
            wx.setStorageSync('userinfo', userinfo);
        } catch (e) {

        }
        wx.navigateBack({
            delta: 1
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getAddressList();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})